package com.xuecheng.search.listener;

import com.xuecheng.search.service.SearchService;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Sober_wxy
 * @date 2022/7/23 17:44
 */
@Component
@Slf4j
public class MessageListener {
        @Autowired
        private SearchService searchService;
        @RabbitListener(
                bindings = @QueueBinding(
                        value = @Queue(
                                value = "course.index.queue"
                        ),
                        exchange = @Exchange(
                                value = "content-exchange",
                                type = ExchangeTypes.TOPIC
                        ),
                        key = "course.*"
                )
        )
        public void updateIndex(Long coursePubId){
                log.info("获取到课程发布消息id={}",coursePubId);
                // 更新es的索引
                try {
                        searchService.createIndex(coursePubId);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        @RabbitListener(
                bindings = @QueueBinding(
                        value = @Queue(
                                value = "course.delIndex.queue"
                        ),
                        exchange = @Exchange(
                                value = "content-exchange",
                                type = ExchangeTypes.TOPIC
                        ),
                        key = "course.*"
                )
        )
        public void deleteIndex(Long coursePubId){
                log.info("获取到课程下架消息id={}",coursePubId);
                // 删除es的索引
                try {
                        searchService.deleteIndex(coursePubId);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}
